package com.jn.glide.views;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.bumptech.glide.load.resource.bitmap.Rotate;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
import com.jn.glide.R;
import com.jn.glide.base.BaseActivity;
import com.jn.glide.contract.MusicContract;
import com.jn.glide.entity.MusicEntity;
import com.jn.glide.presenter.MusicPresenter;
import com.jn.glide.transformation.GrayscaleTransformation;
import com.jn.glide.transformation.VintageFilterTransformation;
import com.jn.glide.transformation.WatermarkTransformation;
import com.jn.glide.utils.LogUtils;

public class MusicActivity extends BaseActivity implements MusicContract.IMusicView, View.OnClickListener {

    private Button mButton;
    private TextView mText;
    private ImageView mImage;
    private ImageView mImage2;
    private TextView mText2;
    private MusicContract.IMusicPresenter mPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_music;
    }

    @Override
    public void init() {
        mButton = findViewById(R.id.button);
        mText = findViewById(R.id.text);
        mImage = findViewById(R.id.image);
        mImage2 = findViewById(R.id.image2);
        mText2 = findViewById(R.id.text2);
        mButton.setOnClickListener(this);
        mPresenter = new MusicPresenter(this);
    }

    @Override
    public void showData(MusicEntity music) {
        LogUtils.d("asdf", "======music======\n" + music.toString());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("歌名 : ").append(music.getName()).append("\n");
        stringBuilder.append("歌手 : ").append(music.getNickname());
        mText.setText(stringBuilder.toString());
        mText2.setText(music.getLyric());
        Glide.with(this)
                .load(music.getPicurl().replace("http", "https"))
                //.dontAnimate() //不显示动画
                .placeholder(R.mipmap.loding) // 占位图
                //.error(R.mipmap.error) //错误时显示的图
                //.fallback(R.drawable.ic_launcher_background)//请求url为null时的占位图
                //.circleCrop() // 圆形裁剪
                //.centerCrop() // 居中裁剪
                //.apply(requestOptions)
                //.apply(RequestOptions.bitmapTransform(new WatermarkTransformation("Glide Demo")))//加水印
                //.apply(RequestOptions.bitmapTransform(new VintageFilterTransformation()))//复古
                //.apply(RequestOptions.bitmapTransform(new GrayscaleTransformation()))//黑白处理
                //.override(500, 500)
                //.transition(DrawableTransitionOptions.withCrossFade(factory))//定义 Glide 如何从占位符到新加载的图片
                //.transform(new CircleCrop()) // 圆形
                //.transform(new RoundedCorners(100)) //圆角
                //.transform(new GranularRoundedCorners(20, 40, 60, 80))//四个角分别设置圆角
                //.transform(new Rotate(90)) //旋转
                //.transform(new GrayscaleTransformation())//黑白
                //.transform(new WatermarkTransformation("Glide Demo"))//自定义
                //.skipMemoryCache(true)//不使用内存缓存
                //.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .into(mImage);

        Glide.with(this)
                .load(music.getPicurl().replace("http", "https"))
                //.dontAnimate() //不显示动画
                //.circleCrop() // 圆形裁剪
                //.centerCrop() // 居中裁剪
                //.apply(requestOptions)
                //.apply(RequestOptions.bitmapTransform(new GrayscaleTransformation()))//黑白处理
                //.override(50, 50)
                //.transition(DrawableTransitionOptions.withCrossFade())//定义 Glide 如何从占位符到新加载的图片 简化写法
                //.transform(new CircleCrop()) // 圆形
                //.transform(new RoundedCorners(100)) //圆角
                //.transform(new GranularRoundedCorners(20, 30, 40, 50))//四个角分别设置圆角
                //.transform(new Rotate(90)) //旋转
                //.transform(new GrayscaleTransformation())//自定义
                //.skipMemoryCache(true)//不使用内存缓存
                //.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .into(mImage2);
    }

    RequestOptions requestOptions = new RequestOptions()//提取出来供多个请求使用
            .placeholder(R.mipmap.loding)
            .error(R.mipmap.error)
            .fallback(R.drawable.ic_launcher_foreground);

    DrawableCrossFadeFactory factory =
            new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();

    @Override
    public void onHttpLoading(boolean loading) {
        if (loading) {
            showLoading();
        } else {
            dismissLoading();
        }
    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            mPresenter.getData();
        }
    }
}